package com.example.demo.test.myThread;

/**
 * 死锁demo
 * 锁资源相互等待
 */
public class DieLockDemo implements Runnable {
    // 定义锁对象
    private static final Object LOCK_A = new Object();
    private static final Object LOCK_B = new Object();

    private Boolean flag;

    public DieLockDemo(Boolean flag) {
        this.flag = flag;
    }

    public DieLockDemo() {

    }

    @Override
    public void run() {
        if (flag) {
            synchronized (LOCK_A) {
                // 得到锁A资源
                System.out.println("if LOCK_A");
                synchronized (LOCK_B) {
                    System.out.println("if LOCK_B");
                }
            }
        } else {
            synchronized (LOCK_B) {
                // 得到锁B资源
                System.out.println("else LOCK_B");
                synchronized (LOCK_A){
                    System.out.println("eles LOCK_A");
                }
            }
        }
    }
}
